import { Injectable } from '@angular/core';
import {RequestUtil} from "../../../../util/request-util";
import {FormUtil} from "../../../../util/form-util";
import {Observable} from "rxjs";
import {MsgResult} from "../../../../models/msg-result";
import {Template} from "./template";
import {MsgAPI} from '../../msg-constants';

@Injectable()
export class TemplateService {

  constructor(
    private request: RequestUtil,
    private formUtil: FormUtil
  ) { }

  //添加实例
  addTemplate(template: Template): Observable<MsgResult> {
    let params = this.formUtil.serialize(template);
    return this.request.post(MsgAPI.insertTemplate, params);
  }

  //删除实例
  deleteTemplate(id: string): Observable<MsgResult> {
    return this.request.delete(MsgAPI.deleteTemplate + "&id=" + id);
  }

  //批量删除
  deleteTemplates(ids: Array<String>): Observable<MsgResult> {
    let params = "idStrs=" + ids;
    return this.request.post(MsgAPI.delTemplates, params);
  }

  //根据id获得规则
  getTemplateById(id: string): Observable<MsgResult> {
    return this.request.get(MsgAPI.getTemplateById + "&id=" + id );
  }

  //修改实例
  updateTemplate(template: Template): Observable<MsgResult> {
    let params = this.formUtil.serialize(template);
    return this.request.put(MsgAPI.updateTemplate,params);
  }

  //无参获取所有记录
  getTemplate(): Observable<MsgResult> {
    return this.request.get(MsgAPI.getTemplate);
  }

}
